//
//  mySettingVC.swift
//  LeDian
//
//  Created by 三秒文化 on 16/3/22.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit
import StoreKit

class mySettingVC: LDBaseCustomNavTabVC {
    
    var logOutBtn:UIButton?
    var skVc:SKStoreProductViewController?
    
    let settingArr = [
        ["提醒设置"],
//        ["修改手机号","修改密码"],
        ["修改密码"],
        ["意见反馈","我要评价","关于我们"],
        ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefaultContent()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewDidLoad()
        
        rdv_tabBarController.setTabBarHidden(true, animated: false)
    }
    
    
    func setDefaultContent() {
        
    
        tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        view.backgroundColor = LDBackGoundColorGray
        self.title = "我的设置"
        
        logOutBtn = UIButton.setDetailsBtn("退出登录", picName: nil, btnBackColor: LDFontColor.White, titleColor: LDFontColor.Red)
        logOutBtn?.userInteractionEnabled = true
        logOutBtn?.titleLabel?.font = LDFont.Font18
        logOutBtn?.frame = CGRectMake(0, 0, LDFrame.ScreenWidth, 48)
        tableView.tableFooterView = logOutBtn
        
        logOutBtn?.bk_whenTapped({ 
            self.tapLogout()
        })
    }
    
    //退出登录Action
    func tapLogout() {
        
        showLDAlertView("确定要退出登录?", sure: {
            // 退出登录
            DDLoginManager.sharedManager().loginOut()
//            LDCacheManager.shareInstance.userCacheManger?.removeAllObjects()
            
            }, cancel: nil)
        
    }
}


extension mySettingVC {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingArr[section].count
        
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return settingArr.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cells:baseMySettingCells?
        cells = tableView.dequeueReusableCellWithIdentifier("cellSetting") as? baseMySettingCells
        if cells == nil {
            cells = baseMySettingCells.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellSetting")
        }
        cells?.titleLab!.text = settingArr[indexPath.section][indexPath.row]
        cells?.divideLine?.hidden = true
        return cells!
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.section == 0 {
            
            switch indexPath.row {
            case 0:
//                let vc = meRemindSettingVC()
                let vc = meRemindSettingVC_Change()
                vc.title = settingArr[0][0]
                navigationController?.pushViewController(vc, animated: true)
            default:
                break
            }
            
        } else if indexPath.section == 1 {
            
            switch indexPath.row {
//            case 0:
//                let vc = mySettingChangPhontNumVC()
//                vc.title = settingArr[1][0]
//                navigationController?.pushViewController(vc, animated: true)
            case 0:
                let vc = reviseSecretVC()
                vc.title = settingArr[1][0]
                navigationController?.pushViewController(vc, animated: true)
            default:
                break
            }
            
        } else if indexPath.section == 2 {
            
            switch indexPath.row {
            case 0:
                let vc = feedBackVC()
                vc.title = settingArr[2][0]
                navigationController?.pushViewController(vc, animated: true)
            case 1:
//                vc.title = settingArr[2][1]//1119078683
                UIApplication.sharedApplication().openURL(NSURL(string: "itms-apps://itunes.apple.com/app/id1119078683")!)
//                skVc = SKStoreProductViewController()
//                skVc!.delegate = self
//                skVc?.loadProductWithParameters([SKStoreProductParameterITunesItemIdentifier:"587767923"],
//                                                completionBlock: { [weak self](isSuccess, error) in
//                                                    if isSuccess {
//                                                        self?.navigationController?.presentViewController((self?.skVc!)!, animated: true, completion: nil)
//                                                        
//                                                    } else {
//                                                        
//                                                    }
//                                    
//                                                    
//                })
                
            case 2:
                let vc = aboutMeVC()
                vc.title = settingArr[2][2]
                navigationController?.pushViewController(vc, animated: true)
            default:
                break
//                print("")
            }
            
        } else{
            
        }
        
    }
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = LDBackGoundColorGray
        return view
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 48
    }
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20
    }
}
extension mySettingVC:SKStoreProductViewControllerDelegate {
    func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
//        navigationController?.dismissViewControllerAnimated(<#T##flag: Bool##Bool#>, completion: nil)
    }
}
